//
//  CollectionViewController.m
//  BeautyClother
//
//  Created by dllo on 15/9/19.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "CollectionController.h"
#import "CollectionViewLayout.h"
#import "AFNetworking.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "ImageSaveViewController.h"
#import "OneCell.h"
#import "ImageCell.h"
#import "FlowCell.h"
#import "IntroduceController.h"
@interface CollectionController ()<UICollectionViewDataSource, UICollectionViewDelegate, ImageCellDelegate>

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) NSMutableDictionary *dic;

@property (nonatomic, retain) UICollectionView *collection;

@property (nonatomic, retain) AFHTTPRequestOperationManager *man;

@property (nonatomic, retain) NSMutableArray *tags;

@property (nonatomic, retain) NSMutableArray *items;

@property (nonatomic, retain) OneCell *one;

@property (nonatomic, retain) NSMutableArray *Num;

@property (nonatomic, retain) CollectionViewLayout *layout;

@property (nonatomic, retain) ImageCell *cell;

@end

@implementation CollectionController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
        self.man = [[AFHTTPRequestOperationManager alloc] init];
        self.tags = [NSMutableArray array];
        self.items = [NSMutableArray array];
        self.one = [[OneCell alloc] init];
        self.Num = [NSMutableArray array];
        self.layout = [[CollectionViewLayout alloc] init];
        self.cell = [[ImageCell alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [_tags release];
    [_items release];
    [_array release];
    [_dic release];
    [_collection release];
    [_Num release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self handleData];
//    [self createCollection];
    
    
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
    
}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleData
{
    NSDictionary *dic = @{@"gf":@"iphone",
                          @"gv":@"6.3.0",
                          @"id":self.aid};
    [self.man GET:MAIN_COLLDETAILE parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = [responseObject objectForKey:@"data"];
        self.tags = [self.dic objectForKey:@"tags"];
        self.array = [self.dic objectForKey:@"itemPicUrlList"];
        
        CGFloat width = [[self.dic objectForKey:@"width"] floatValue];
        CGFloat height = [[self.dic objectForKey:@"height"] floatValue];
        self.layout.height =  (self.view.frame.size.width - 10) * height / width;
        [self createCollection];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)createCollection
{
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:_layout];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"image"];
    [self.collection registerClass:[OneCell class] forCellWithReuseIdentifier:@"one"];
    [self.collection registerClass:[ImageCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collection registerClass:[FlowCell class] forCellWithReuseIdentifier:@"flow"];
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection release];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0 || section == 1 || section == 2) {
        return 1;
    }else
    {
        if (self.Num.count == 0) {
            self.Num = [[[self.dic objectForKey:@"items"] objectAtIndex:0] objectForKey:@"itemList"];
            self.layout.dicArray = self.Num;
            return [[[[self.dic objectForKey:@"items"] objectAtIndex:0] objectForKey:@"itemList"] count];
        } else
        {
            return self.Num.count;
        }
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];
        
        
        CGFloat width = [[self.dic objectForKey:@"width"] floatValue];
        CGFloat height = [[self.dic objectForKey:@"height"] floatValue];
        
        CGFloat RealHeight =  (self.view.frame.size.width - 10) * height / width;
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, self.view.frame.size.width - 10, RealHeight)];
        NSString *pic = [self.dic objectForKey:@"picUrl"];
        NSArray *arr = [pic componentsSeparatedByString:@"?"];
        [image sd_setImageWithURL:[NSURL URLWithString:[arr objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        image.userInteractionEnabled = YES;
        UITapGestureRecognizer *save = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(save:)];
        [image addGestureRecognizer:save];
        [cell.contentView addSubview:image];
        return  cell;
    }
    if (indexPath.section == 1) {
        self.one = [collectionView dequeueReusableCellWithReuseIdentifier:@"one" forIndexPath:indexPath];
        
        NSString *pic = [self.dic objectForKey:@"userAvatar"];
        NSArray *arr = [pic componentsSeparatedByString:@"?"];
        [self.one.avatar sd_setImageWithURL:[NSURL URLWithString:[arr objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        self.one.username.text = [self.dic objectForKey:@"userName"];
        [self.one.date setTitle:[self.dic objectForKey:@"dateTime"] forState:UIControlStateNormal];
        [self.one sendArr:self.tags];
        
        self.one.desc.text = [self.dic objectForKey:@"description"];
        return self.one;
    }
    if (indexPath.section == 2) {
       self.cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        _cell.array = self.array;
        _cell.delegate = self;
        return _cell;
    }
    else{
        FlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"flow" forIndexPath:indexPath];
        NSString *pic = [[[self.Num objectAtIndex:indexPath.item] objectForKey:@"component"] objectForKey:@"picUrl"];
        NSArray *arr = [pic componentsSeparatedByString:@"?"];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:arr[0]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        cell.image.userInteractionEnabled = YES;
        return cell;
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        IntroduceController *intro = [[IntroduceController alloc] init];
        intro.Inid = [[[self.Num objectAtIndex:indexPath.item] objectForKey:@"component"] objectForKey:@"id"];
        intro.source_id = [[[[self.Num objectAtIndex:indexPath.item] objectForKey:@"component"] objectForKey:@"action"] objectForKey:@"sourceId"];
        [self.navigationController pushViewController:intro animated:YES];
    }
}

- (void)passNum:(NSInteger)num
{
    self.items = [self.dic objectForKey:@"items"];
    self.Num = [[self.items objectAtIndex:num] objectForKey:@"itemList"];
    self.layout.dicArray = self.Num;
    [self.collection reloadData];
}

- (void)save:(UITapGestureRecognizer *)save
{
    ImageSaveViewController *iiii = [[ImageSaveViewController alloc]init];
    iiii.image = ((UIImageView *)save.view).image;
    CGFloat height = [[self.dic objectForKey:@"height"]floatValue];
    CGFloat width = [[self.dic objectForKey:@"width"]floatValue];
    iiii.width = width;
    iiii.height = height;
    [self.navigationController pushViewController:iiii animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
